﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Net.Mail;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using System.Windows.Forms;
using DRHotel_v1.Classes;
using DRHotel_v1.Classes.DBManagers;

namespace DRHotel_v1.RoomAvaAndBooking
{
    public partial class CheckInFrm : Form
    {
        public bool isFromRes = false;//if user is directed from reservation record form, this is true. if not, false
        public static ArrayList avaRoomList;
        public static string roomID;
        public Reservation res;
        public Customer cus;
        public Room room;
        public string roomIDFromResRecordFrm;
        public string cusIC;
        public bool checkICStatus;//true --> existing IC

        public CheckInFrm()
        {
            InitializeComponent();
        }

        #region Check Room Availability
        private void btnCheckRoomAva_Click(object sender, EventArgs e)
        {
            DateTime desCheckOutDate = dtpDesCheckOut.Value;
            string roomType = cbbRoomType.Text;
            string bedType = cbbBedType.Text;
            bool smoking = cbxSmoking.Checked;

            avaRoomList = RoomAvaAndBookingDBManager.GetAvaRooms(roomType, bedType, smoking);
            AvaRoomsFrm frm = new AvaRoomsFrm();
            frm.avaRoomsDS = avaRoomList;
            frm.ShowDialog();
            tbxRoomNo.Text = roomID;
        }
        #endregion

        #region Form Load
        private void CheckInFrm_Load(object sender, EventArgs e)
        {
            cbbAdult.SelectedIndex = 0;
            cbbChildren.SelectedIndex = 0;
            cbbRoomType.DataSource = RoomAvaAndBookingDBManager.GetRoomType();
            cbbBedType.DataSource = RoomAvaAndBookingDBManager.GetBedType();
            if (roomIDFromResRecordFrm.Length != 0)//check if user is redirect from reservation record form
            {
                tbxRoomNo.Text = roomIDFromResRecordFrm;
                cbbAdult.Text = res.numOfAdult.ToString();
                cbbChildren.Text = res.numOfChildren.ToString();
                dtpDesCheckOut.Value = res.checkOutDateTime;
            }
            else
            {
                cbbAdult.SelectedIndex = 0;
                cbbChildren.SelectedIndex = 0;
                cbbICType.SelectedIndex = 0;
                cbbCustomerType.SelectedIndex = 0;

                dtpDesCheckOut.Format = DateTimePickerFormat.Custom;
                dtpDesCheckOut.CustomFormat = "dd-MMM-yyyy";
            }

            //load customer if existing
            if (cusIC.Length != 0)
            {
                //get the customer and fill form using existing data
                cus = RoomAvaAndBookingDBManager.GetCusByIC(cusIC);
                tbxICNo.Text = cus.cusIC;
                cbbICType.Text = cus.ICType;
                tbxFirstName.Text = cus.firstName;
                tbxLastName.Text = cus.lastName;
                if (cus.gender == true)
                {
                    rbnMale.Checked = true;
                }
                else
                {
                    rbnFemale.Checked = true;
                }
                cbbCustomerType.Text = cus.cusType;
                dtpDOB.Value = cus.dob;
                tbxCountry.Text = cus.country;
                tbxPhone.Text = cus.phoneNo;
                tbxEmail.Text = cus.email;
                tbxAddress.Text = cus.mailingAddress;
                tbxPostal.Text = cus.postCode;
            }
        }
        #endregion

        #region Check In Button Click
        private void btnCheckIn_Click(object sender, EventArgs e)
        {
            #region Empty Input validation
            if (tbxRoomNo.Text.Trim() == string.Empty)//check if room number empty
            {
                MessageBox.Show("Empty Room No!", "Alert", MessageBoxButtons.OK, MessageBoxIcon.Information);
                tbxRoomNo.SelectAll();
                tbxRoomNo.Focus();
                return;
            }
            if (tbxICNo.Text.Trim() == string.Empty)//check if IC number is empty
            {
                MessageBox.Show("Empty IC Type!", "Alert", MessageBoxButtons.OK, MessageBoxIcon.Information);
                errorProvider1.SetError(tbxICNo, "Empty IC Number!");
                return;
            }
            if (cbbICType.Text.Trim() == string.Empty)//check if IC type is empty
            {
                MessageBox.Show("Empty IC Type!", "Alert", MessageBoxButtons.OK, MessageBoxIcon.Information);
                cbbICType.SelectAll();
                cbbICType.Focus();
                return;
            }
            if (cbbCustomerType.Text.Trim() == string.Empty)//check if customer number is empty
            {
                MessageBox.Show("Empty Customer Type!", "Alert", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
            if (cbbAdult.Text.Trim() == string.Empty)//check if adult number is empty
            {
                MessageBox.Show("Empty Number of Adults!", "Alert", MessageBoxButtons.OK, MessageBoxIcon.Information);

                return;
            }
            if (cbbChildren.Text.Trim() == string.Empty)//check if children number is empty
            {
                MessageBox.Show("Empty Number of Children!", "Alert", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
            if (tbxFirstName.Text.Trim() == string.Empty)
            {
                MessageBox.Show("Empty First Name!", "Alert", MessageBoxButtons.OK, MessageBoxIcon.Information);
                errorProvider1.SetError(tbxFirstName, "Empty First Name!");
                return;
            }
            if (tbxLastName.Text.Trim() == string.Empty)
            {
                MessageBox.Show("Empty Last Name!", "Alert", MessageBoxButtons.OK, MessageBoxIcon.Information);
                errorProvider1.SetError(tbxLastName, "Empty Last Name!");
                return;
            }
            if (tbxCountry.Text.Trim() == string.Empty)
            {
                MessageBox.Show("Empty Country!", "Alert", MessageBoxButtons.OK, MessageBoxIcon.Information);
                errorProvider1.SetError(tbxCountry, "Empty Country!");
                return;
            }

            if (tbxEmail.Text.Trim() == string.Empty)
            {
                MessageBox.Show("Empty Email Address!", "Alert", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
            else if (!IsValidEmailAddress(tbxEmail.Text))
            {
                MessageBox.Show("Invalid Email Address!", "Alert", MessageBoxButtons.OK, MessageBoxIcon.Information);
                tbxEmail.SelectAll();
                tbxEmail.Focus();
                return;
            }

            #endregion

            #region fill and change classes info
            //create Customer and fill info
            cus = new Customer();
            cus.cusIC = tbxICNo.Text;
            cus.ICType = cbbICType.Text;
            cus.firstName = tbxFirstName.Text;
            cus.lastName = tbxLastName.Text;
            if (rbnFemale.Checked)
            {
                cus.gender = false;
            }
            else if (rbnMale.Checked)
            {
                cus.gender = true;
            }
            cus.cusType = cbbCustomerType.Text;
            //string dobString = cbbDay.Text + "-" + cbbMonth.Text + "-" + cbbYear.Text;
            //DateTime dob = Convert.ToDateTime(dobString);
            cus.dob = dtpDOB.Value;
            cus.country = tbxCountry.Text;
            cus.phoneNo = tbxPhone.Text;
            cus.email = tbxEmail.Text;
            cus.mailingAddress = tbxAddress.Text;
            cus.postCode = tbxPostal.Text;

            //fill Reservation class
            if (roomIDFromResRecordFrm == null)
            {
                res = new Reservation();
            }
            res.checkOutDateTime = Convert.ToDateTime(dtpDesCheckOut.Text);
            res.checkInDateTime = DateTime.Now;
            res.roomID = Convert.ToInt32(tbxRoomNo.Text);
            res.cusIC = tbxICNo.Text;
            res.numOfAdult = Convert.ToInt32(cbbAdult.Text);
            res.numOfChildren = Convert.ToInt32(cbbChildren.Text);
            res.remark = tbxRemark.Text;
            res.resStatus = "Staying";
            res.recordDateTime = DateTime.Now;

            #endregion

            #region Insert into DB
            if (checkICStatus == true)
            {
                if (RoomAvaAndBookingDBManager.UpdateCustomerInfo(cus) == 0)
                {
                    MessageBox.Show("Customer Recorded Failed!", "Alert", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    return;
                }
            }
            else
            {
                if (RoomAvaAndBookingDBManager.InsertCustomer(cus) == 0)
                {
                    MessageBox.Show("Customer Recorded Failed!", "Alert", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    return;
                }
            }
            if (isFromRes == true)
            {
                if (RoomAvaAndBookingDBManager.UpdateReservationInfo(res) == 0)//have to insert customer first because of DB dependency
                {
                    MessageBox.Show("Reservation Created Failed!", "Alert", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    return;
                }
            }
            else
            {
                if (RoomAvaAndBookingDBManager.InsertReservation(res) == 0)//have to insert customer first because of DB dependency
                {
                    MessageBox.Show("Reservation Created Failed!", "Alert", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    return;
                }
            }
            if (RoomAvaAndBookingDBManager.UpdateRoomStatus(tbxRoomNo.Text, "Occupied") == 0)//Change Room Status
            {
                MessageBox.Show("Room Status Updated Failed!", "Alert", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
            else
            {
                MessageBox.Show("Check In Sucessful!", "Alert", MessageBoxButtons.OK, MessageBoxIcon.Information);
                this.Close();
                //============================>>>display reservation record form
            }
            #endregion

        }
        #endregion

        #region Check If Customer IC is existing
        private void btnCheckCusIC_Click(object sender, EventArgs e)
        {
            if (RoomAvaAndBookingDBManager.SearchExistingCus(tbxICNo.Text) == true)//check if customer is existing
            {
                checkICStatus = true;
                //get the customer and fill form using existing data
                cus = RoomAvaAndBookingDBManager.GetCusByIC(tbxICNo.Text);
                cbbICType.Text = cus.ICType;
                tbxFirstName.Text = cus.firstName;
                tbxLastName.Text = cus.lastName;
                if (cus.gender == true)
                {
                    rbnMale.Checked = true;
                }
                else
                {
                    rbnFemale.Checked = true;
                }
                cbbCustomerType.Text = cus.cusType;
                dtpDOB.Value = cus.dob;
                tbxCountry.Text = cus.country;
                tbxPhone.Text = cus.phoneNo;
                tbxEmail.Text = cus.email;
                tbxAddress.Text = cus.mailingAddress;
                tbxPostal.Text = cus.postCode;
            }
            else
            {
                checkICStatus = false;
                btnCheckCusIC.Text = "Proceed";
                btnCheckCusIC.ForeColor = Color.White;
                btnCheckCusIC.BackColor = Color.Green;
            }
        }
        #endregion

        #region Dynamically Change Combobox Bed Type according selected Room Type
        private void cbbRoomType_SelectedIndexChanged(object sender, EventArgs e)
        {
            cbbBedType.DataSource = RoomAvaAndBookingDBManager.GetBedTypeByRoomType(cbbRoomType.Text);
        }

        private void cbbBedType_SelectedIndexChanged(object sender, EventArgs e)
        {
            //cbbRoomType.DataSource = RoomAvaAndBookingDBManager.GetRoomTypeByBedType(cbbBedType.Text);
        }
        #endregion

        #region Input Validation
        private void tbxICNo_KeyPress(object sender, KeyPressEventArgs e)//validate IC no
        {
            Regex regex = new Regex("^[A-Z0-9]*$");
            if (regex.IsMatch(tbxICNo.Text))
            {
                errorProvider1.SetError(tbxICNo, String.Empty);
            }
            else
            {
                errorProvider1.SetError(tbxICNo, "Only capital letters and numbers may be entered here");
            }
        }

        private void tbxICNo_Validating(object sender, CancelEventArgs e)//incase of copy and past
        {
            Regex regex = new Regex("^[A-Z0-9]*$");
            if (regex.IsMatch(tbxICNo.Text))
            {
                errorProvider1.SetError(tbxICNo, String.Empty);
            }
            else
            {
                errorProvider1.SetError(tbxICNo, "Only capital letters and numbers may be entered here");
            }
        }

        public bool IsValidEmailAddress(string email)//validate email address
        {
            try
            {
                MailAddress ma = new MailAddress(email);
                return true;
            }
            catch
            {
                return false;
            }
        }

        private void tbxPhone_TextChanged(object sender, EventArgs e)
        {
            Regex regex = new Regex("^[0-9]+$");
            if (regex.IsMatch(tbxPhone.Text))
            {
                errorProvider1.SetError(tbxPhone, String.Empty);
            }
            else
            {
                errorProvider1.SetError(tbxPhone, "Only numbers may be entered here");
            }
        }

        private void tbxPostal_TextChanged(object sender, EventArgs e)
        {
            Regex regex = new Regex("^[A-Z0-9]*$");
            if (regex.IsMatch(tbxPostal.Text))
            {
                errorProvider1.SetError(tbxPostal, String.Empty);
            }
            else
            {
                errorProvider1.SetError(tbxPostal, "Only capital letters and numbers may be entered here");
            }
        }

        #endregion

        #region remove errorProvider1
        private void tbxFirstName_KeyPress(object sender, KeyPressEventArgs e)
        {
            errorProvider1.SetError(tbxFirstName, String.Empty);
        }

        private void tbxLastName_KeyPress(object sender, KeyPressEventArgs e)
        {
            errorProvider1.SetError(tbxLastName, String.Empty);
        }

        private void tbxCountry_KeyPress(object sender, KeyPressEventArgs e)
        {
            errorProvider1.SetError(tbxCountry, String.Empty);
        }
        #endregion

    }
}
